package ldh.fx.adminfx.ui;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import ldh.fx.adminfx.cell.ObjectListCell;
import ldh.fx.adminfx.cell.ObjectTableCellFactory;
import ldh.fx.adminfx.commpont.LPane;
import ldh.fx.adminfx.commpont.chart.smooth.SmoothedChart;
import ldh.fx.adminfx.commpont.grid.NoHeaderTableView;
import ldh.fx.adminfx.pojo.Product;
import ldh.fx.adminfx.pojo.User;
import ldh.fx.adminfx.util.TableViewUtil;
import org.kordamp.ikonli.fontawesome.FontAwesome;
import org.kordamp.ikonli.ionicons.Ionicons;
import org.kordamp.ikonli.javafx.FontIcon;
import org.kordamp.ikonli.javafx.StackedFontIcon;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

public class BootstrapIndexPane extends VBox {

    private StringProperty data1Property = new SimpleStringProperty();
    private StringProperty data2Property = new SimpleStringProperty();
    private StringProperty data3Property = new SimpleStringProperty();
    private StringProperty data4Property = new SimpleStringProperty();

    public BootstrapIndexPane() {
        this.getStyleClass().add("chart-pane");
        buildShipments();
        buildDatasPane();
        buildListPane();
    }

    private void buildListPane() {
        VBox box1 = new VBox();
        box1.getStyleClass().addAll("block-pane", "sell-product");

        Label title = new Label("Transactions");
        title.getStyleClass().add("title");

        Label t1 = new Label("Revenue ($3189)");
        t1.getStyleClass().add("data-label");
        Label t2 = new Label("Revenue (%)");
        t2.getStyleClass().add("data-label");
        Region region = new Region();
        HBox.setHgrow(region, Priority.ALWAYS);
        HBox headBox = new HBox(20, title, region, t1, t2);
        box1.getChildren().add(headBox);

        TableView<Product> productNoHeaderTableView = new NoHeaderTableView<>();
        productNoHeaderTableView.getStyleClass().add("product-table-view");

        TableColumn<Product, String> pictureColumn = new TableColumn<>("产品图片");
        pictureColumn.setCellValueFactory(new PropertyValueFactory("img"));
        pictureColumn.setPrefWidth(60);
        pictureColumn.setCellFactory(new ObjectTableCellFactory<Product, String>(s->{
            ImageView imageView = new ImageView();
            imageView.setFitWidth(50d);
            imageView.setFitHeight(50d);
            imageView.setImage(new Image(s));
            return imageView;
        }));

        TableColumn<Product, Product> productInfoColumn = new TableColumn<>("产品信息");
        productInfoColumn.setCellValueFactory(new PropertyValueFactory("pinfo"));
        productInfoColumn.setCellFactory(new ObjectTableCellFactory<>(p->{
            VBox vBox = new VBox(new Label(p.getInfo()), new Label(p.getPrize()+""));
            vBox.setAlignment(Pos.CENTER_LEFT);
            return vBox;
        }));
        productInfoColumn.setPrefWidth(150);
        TableColumn<Product, String> prizeColumn = new TableColumn<>("价格");
        prizeColumn.setPrefWidth(80);
        prizeColumn.setCellValueFactory(new PropertyValueFactory("prize"));
        TableColumn<Product, Double> revenueColumn = new TableColumn<>("进度");
        revenueColumn.setPrefWidth(180);
        revenueColumn.setCellValueFactory(new PropertyValueFactory("progress"));
        revenueColumn.setCellFactory(new ObjectTableCellFactory<Product, Double>(s->{
            HBox hBox = new HBox(10);
            hBox.setMouseTransparent(true);
            hBox.setAlignment(Pos.CENTER_LEFT);
            ProgressBar progressBar = new ProgressBar(s/100.0);
            progressBar.getStyleClass().add("progress-bar-primary");
            Label label = new Label(s +"%");
            hBox.getChildren().addAll(progressBar, label);
            return hBox;
        }, Pos.CENTER));
        productNoHeaderTableView.getColumns().addAll(pictureColumn, productInfoColumn, prizeColumn, revenueColumn);
        TableViewUtil.width(productNoHeaderTableView);
        TableViewUtil.alignment(Pos.CENTER_LEFT, prizeColumn);

        productNoHeaderTableView.getItems().addAll(loadProductData());
        box1.getChildren().add(productNoHeaderTableView);


        VBox box2 = new VBox();
        box2.getStyleClass().addAll("block-pane", "active-user");

        HBox hbox2 = new HBox();
        Label title2 = new Label("活跃用户");
        Region region2 = new Region();
        HBox.setHgrow(region2, Priority.ALWAYS);
        FontIcon fontIcon = FontIcon.of(Ionicons.ION_ANDROID_MORE_HORIZONTAL, 24, Color.WHITE);
        hbox2.getChildren().addAll(title2, region2, fontIcon);

        ListView<User> userListView = new ListView<User>();
        userListView.setCellFactory(new ObjectListCell<>(user -> {
            HBox hBox = new HBox(20);
            ImageView imageView = new ImageView();
            imageView.setFitHeight(60);
            imageView.setFitWidth(60);
            imageView.setImage(new Image(user.getImg()));

            VBox vBox = new VBox(10);
            HBox.setHgrow(vBox, Priority.ALWAYS);
            vBox.getChildren().addAll(new Label(user.getName()), new Label(user.getSex() == 1 ? "男" : "女"));

            hBox.getChildren().addAll(imageView, vBox);
            return hBox;
        }));
        userListView.getItems().addAll(loadUsers());
        box2.getChildren().addAll(hbox2, userListView);

        GridPane gridPane = new GridPane();
        gridPane.getChildren().addAll(box1, box2);

        int row=0, col = 0;
        GridPane.setConstraints(box1, col++, row);
        GridPane.setConstraints(box2, col, row);
        ColumnConstraints c1 = new ColumnConstraints();
        ColumnConstraints c2 = new ColumnConstraints();
        gridPane.getColumnConstraints().addAll(c1, c2);
        this.widthProperty().addListener((ob, o, n)->{
            c1.setPrefWidth(BootstrapIndexPane.this.getWidth()/3 *2);
            c2.setPrefWidth(BootstrapIndexPane.this.getWidth()/3);
        });

        this.getChildren().add(gridPane);
    }

    // 构建数据信息
    private void buildDatasPane() {
        HBox hBox = new HBox();
        hBox.setPrefHeight(150);
        hBox.setSpacing(20);
        FontIcon fontIcon1 = FontIcon.of(FontAwesome.findByDescription(FontAwesome.USER.getDescription()), Color.YELLOWGREEN);
        FontIcon fontIcon2 = FontIcon.of(FontAwesome.findByDescription(FontAwesome.REFRESH.getDescription()));
        data1Property.set("150GB");
        Pane pane1 = buildDataPane(fontIcon1, "Number", data1Property, fontIcon2, "Update Now");

        fontIcon1 = FontIcon.of(FontAwesome.findByDescription(FontAwesome.WEIBO.getDescription()), Color.BLUEVIOLET);
        fontIcon2 = FontIcon.of(FontAwesome.findByDescription(FontAwesome.LINE_CHART.getDescription()));
        data2Property.set("+45k");
        Pane pane2 = buildDataPane(fontIcon1, "Followers", data2Property, fontIcon2, "Last Research");

        fontIcon1 = FontIcon.of(FontAwesome.findByDescription(FontAwesome.USER.getDescription()), Color.GREEN);
        fontIcon2 = FontIcon.of(FontAwesome.findByDescription(FontAwesome.TABLE.getDescription()));
        data3Property.set("150,000");
        Pane pane3 = buildDataPane(fontIcon1, "Users", data3Property, fontIcon2, "Customers feedback");

        fontIcon1 = FontIcon.of(FontAwesome.findByDescription(FontAwesome.SEARCH.getDescription()), Color.RED);
        fontIcon2 = FontIcon.of(FontAwesome.findByDescription(FontAwesome.SEARCH.getDescription()));
        data4Property.set("11");
        Pane pane4 = buildDataPane(fontIcon1, "Errors", data4Property, fontIcon2, "In the last hours");
        HBox.setHgrow(pane1, Priority.ALWAYS);
        HBox.setHgrow(pane2, Priority.ALWAYS);
        HBox.setHgrow(pane3, Priority.ALWAYS);
        HBox.setHgrow(pane4, Priority.ALWAYS);
        hBox.getChildren().addAll(pane1, pane2, pane3, pane4);
        this.getChildren().add(hBox);
    }

    private Pane buildDataPane(FontIcon fontIcon, String title, StringProperty valueProperty, FontIcon tipFontIcon, String desc) {
        VBox vBox = new VBox();

        vBox.getStyleClass().add("block-pane");
        HBox hBox = new HBox();

        hBox.setAlignment(Pos.CENTER_LEFT);
        Region region = new Region();
        HBox.setHgrow(region, Priority.ALWAYS);
        VBox titlePane = new VBox();
        titlePane.setAlignment(Pos.CENTER);
        Label valueLabel = new Label();
        valueLabel.getStyleClass().add("data-value");
        valueLabel.textProperty().bind(valueProperty);

        Label titleLabel = new Label(title);
        titleLabel.getStyleClass().add("data-label");
        titlePane.getChildren().addAll(titleLabel, valueLabel);
        StackedFontIcon stackedFontIcon = new StackedFontIcon();
        FontIcon circle = FontIcon.of(FontAwesome.findByDescription(FontAwesome.CIRCLE.getDescription()), 24, Color.RED);
        stackedFontIcon.setIconCodes(circle.getIconCode(), fontIcon.getIconCode());
        stackedFontIcon.setIconSizes(4, 3);
        Color color = (Color) fontIcon.getIconColor();
        stackedFontIcon.setColors(color.deriveColor(0, 0, 0.2, 1), color);
        hBox.getChildren().addAll(stackedFontIcon, region, titlePane);
        VBox.setVgrow(hBox, Priority.ALWAYS);

        HBox bottomPane = new HBox();
        bottomPane.setSpacing(10);
        bottomPane.getChildren().addAll(tipFontIcon, new Label(desc));
        bottomPane.setAlignment(Pos.CENTER_LEFT);
        bottomPane.getStyleClass().add("data-icon-pane");
        vBox.getChildren().addAll(hBox, new Separator(), bottomPane);
        return vBox;
    }

    //构建售卖信息
    private void buildShipments() {
        VBox titlePane = new VBox();
        titlePane.getStyleClass().add("block-pane");

        // 构建表头
        HBox titleHBox = new HBox();

        Label titleLabel = new Label("Total Shipments");
        titleLabel.getStyleClass().add("data-label");

        Label subTitleLabel = new Label("Performance");
        subTitleLabel.getStyleClass().add("data-value");
        VBox titleVBox = new VBox(titleLabel, subTitleLabel);

        Region region = new Region();
        HBox.setHgrow(region, Priority.ALWAYS);

        ToggleGroup  toggleGroup = new ToggleGroup();

        HBox buttonHbox = new HBox();
        buttonHbox.getStyleClass().add("button-group");
        ToggleButton accountBtn = new ToggleButton("Accounts");
        ToggleButton purchaseBtn = new ToggleButton("Purchase");
        ToggleButton sessionBtn = new ToggleButton("Session");
        accountBtn.setToggleGroup(toggleGroup);
        purchaseBtn.setToggleGroup(toggleGroup);
        sessionBtn.setToggleGroup(toggleGroup);
        buttonHbox.getChildren().addAll(accountBtn, purchaseBtn, sessionBtn);
        titleHBox.getChildren().addAll(titleVBox, region, buttonHbox);

        titlePane.getChildren().add(titleHBox);

        // 构建图形
        NumberAxis yAxis = new NumberAxis(60, 130, 10);
        yAxis.setTickMarkVisible(false);
        yAxis.setMinorTickVisible(false);
        yAxis.setVisible(false);
        CategoryAxis xAxis = new CategoryAxis(FXCollections.observableArrayList("一月份", "二月份", "三月份", "四月份", "五月份", "六月份", "七月份", "八月份", "九月份", "十月份", "十一月份", "十二月份"));
        SmoothedChart<String, Number> lineChart = new SmoothedChart<>(xAxis, yAxis);
        loadPurchaseData(lineChart, "1");
        lineChart.setChartType(SmoothedChart.ChartType.LINE);
        titlePane.getChildren().add(lineChart);
        titlePane.setPrefHeight(250);

        this.getChildren().add(titlePane);
    }

    private void loadPurchaseData(SmoothedChart<String, Number> chart, String type) {
        if (type.equals("1")) {
            List<XYChart.Data<String, Number>> dataList = new ArrayList();
            dataList.add(new XYChart.Data<>("一月份", 100));
            dataList.add(new XYChart.Data<>("二月份", 80));
            dataList.add(new XYChart.Data<>("三月份", 66));
            dataList.add(new XYChart.Data<>("四月份", 98));
            dataList.add(new XYChart.Data<>("五月份", 120));
            dataList.add(new XYChart.Data<>("六月份", 105));
            dataList.add(new XYChart.Data<>("七月份", 110));
            dataList.add(new XYChart.Data<>("八月份", 126));
            dataList.add(new XYChart.Data<>("九月份", 98));
            dataList.add(new XYChart.Data<>("十月份", 120));
            dataList.add(new XYChart.Data<>("十一月份", 89));
            dataList.add(new XYChart.Data<>("十二月份", 78));

            XYChart.Series<String, Number> series = new XYChart.Series<>(FXCollections.observableArrayList(dataList));
            chart.setSymbolSize(series, 10);
            chart.setData(FXCollections.observableList(Arrays.asList(series)));
        }
    }

    private List<Product> loadProductData() {
        List<Product> products = new ArrayList<>();
        for(int i=0; i<10; i++) {
            Product product = new Product();
            product.setImg(this.getClass().getResource("/img/48.jpg").toExternalForm());
            product.setInfo("demo " + i);
            product.setPrize((int)(Math.random() * 100000)/100.0);
            product.setProgress((int)(Math.random() * 10000)/100.0);
            products.add(product);
        }
        return products;
    }

    private List<User> loadUsers() {
        List<User> users = new ArrayList<>();
        for(int i=0; i<10; i++) {
            User user = new User();
            user.setImg(this.getClass().getResource("/img/fans/t1.jpg").toExternalForm());
            user.setName("demo " + i);
            user.setSex((int)(Math.random() * 100)/2);
            user.setAge((int)(Math.random() * 60));
            users.add(user);
        }
        return users;
    }
}
